package com.retail.shopping.server.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.retail.common.log.annotation.Log;
import com.retail.common.log.enums.BusinessType;
import com.retail.common.security.annotation.RequiresPermissions;
import com.retail.shopping.common.domain.ShoppingCart;
import com.retail.shopping.server.service.IShoppingCartService;
import com.retail.common.core.web.controller.BaseController;
import com.retail.common.core.domain.Result;
import com.retail.common.core.utils.poi.ExcelUtil;
import com.retail.common.core.web.page.TableDataInfo;

/**
 * 购物车主Controller
 * 
 * @author retail
 * @date 2024-02-27
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController extends BaseController
{
    @Autowired
    private IShoppingCartService shoppingCartService;
    /**
     *
     * 购物车数据获取
     *  用户点击购物车
     *      发送请求至后端后端获取到用户的编号
     *  servlet层
     *      根据用户的编号查询购物车的id
     *      获取购物车信息
     *
     *      加一个判断用户是否存入到redis
     *      没有 查询购物车信息存入到redis并返回
     *      存在 直接在redis中获取到用户的购物车信息
     *  mapper层获取到所需的字段
     *      返回一个List对象
     *
     * */

    /**
     * 查询购物车主列表
     */
    @RequiresPermissions("shopping:shoppingCart:list")
    @GetMapping("/list")
    public TableDataInfo list(ShoppingCart shoppingCart)
    {
        startPage();
        List<ShoppingCart> list = shoppingCartService.selectShoppingCartList(shoppingCart);
        return getDataTable(list);
    }

    /**
     * 导出购物车主列表
     */
    @RequiresPermissions("shopping:shoppingCart:export")
    @Log(title = "购物车主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShoppingCart shoppingCart)
    {
        List<ShoppingCart> list = shoppingCartService.selectShoppingCartList(shoppingCart);
        ExcelUtil<ShoppingCart> util = new ExcelUtil<ShoppingCart>(ShoppingCart.class);
        util.exportExcel(response, list, "购物车主数据");
    }

    /**
     * 获取购物车主详细信息
     */
    @RequiresPermissions("shopping:shoppingCart:query")
    @GetMapping(value = "/{cartId}")
    public Result getInfo(@PathVariable("cartId") Long cartId)
    {
        return success(shoppingCartService.selectShoppingCartByCartId(cartId));
    }

    /**
     * 新增购物车主
     */
    @RequiresPermissions("shopping:shoppingCart:add")
    @Log(title = "购物车主", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody ShoppingCart shoppingCart)
    {
        return toAjax(shoppingCartService.insertShoppingCart(shoppingCart));
    }

    /**
     * 修改购物车主
     */
    @RequiresPermissions("shopping:shoppingCart:edit")
    @Log(title = "购物车主", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody ShoppingCart shoppingCart)
    {
        return toAjax(shoppingCartService.updateShoppingCart(shoppingCart));
    }

    /**
     * 删除购物车主
     */
    @RequiresPermissions("shopping:shoppingCart:remove")
    @Log(title = "购物车主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cartIds}")
    public Result remove(@PathVariable Long[] cartIds)
    {
        return toAjax(shoppingCartService.deleteShoppingCartByCartIds(cartIds));
    }

}
